// device/list.js
Page({
  data: {
    devices: [],
    currentPage: 1,
    pageSize: 10,
    hasMore: true
  },

  onLoad() {
    this.loadDevices()
  },

  loadDevices() {
    // 模拟加载设备列表
    const devices = [
      {
        id: 1,
        name: '充电宝001',
        location: '商场一楼',
        distance: '100m',
        battery: 80,
        status: 1 // 1:可用, 0:不可用
      },
      {
        id: 2,
        name: '充电宝002',
        location: '餐厅门口',
        distance: '150m',
        battery: 90,
        status: 1
      },
      {
        id: 3,
        name: '充电宝003',
        location: '电影院旁',
        distance: '200m',
        battery: 60,
        status: 1
      }
    ]

    this.setData({
      devices: this.data.devices.concat(devices)
    })
  },

  onDeviceClick(e) {
    const deviceId = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/device/detail?id=${deviceId}`
    })
  },

  onScanCode() {
    wx.scanCode({
      success: (res) => {
        console.log('扫描结果', res)
        // 处理扫描结果，跳转到租借确认页面
        wx.navigateTo({
          url: '/pages/rental/confirm?deviceId=' + res.result
        })
      },
      fail: (err) => {
        console.error('扫描失败', err)
        wx.showToast({
          title: '扫描失败',
          icon: 'none'
        })
      }
    })
  }
})